<!--
 * @Author: ChaiHongJun
 * @Date: 2021-03-25 16:03:08
 * @LastEditors: ChaiHongJun
 * @LastEditTime: 2021-06-08 15:48:04
 * @version: 
 * @Description: 歌单模板
-->
<template>
  <div class="playlist-wrap container">
    <div class="filter shadow">
      <div class="title flex-center" @click="showMenus = !showMenus">
        {{ currentCat }}
        <i class="iconfont niceiconfontyoujiantou-copy-copy-copy-copy"></i>
        <transition name="fade">
          <div class="filter-box shadow" v-if="showMenus">
            <div
              class="item"
              v-for="(Category, key) in getCategories"
              :key="key"
            >
              <h2>
                <i class="iconfont niceyuyan"></i>
                {{ Category }}
              </h2>
              <ul>
                <li
                  v-for="(subItem, index) in getSonTags(key)"
                  :key="index"
                  :class="{ active: currentCat == subItem.name }"
                  @click="
                    getSpecifiedTagsPlaylist(
                      index,
                      pageSize,
                      offset,
                      subItem.name
                    )
                  "
                >
                  {{ subItem.name }}
                </li>
              </ul>
            </div>
          </div>
        </transition>
      </div>
      <div class="hot-tag flex-row">
        <p>热门标签：</p>
        <ul class="flex-center">
          <li
            v-for="hotTag of getTags"
            :key="hotTag.id"
            :class="{ active: currentCat == hotTag.name }"
            @click="
              getSpecifiedTagsPlaylist(pageSize, offset, order, hotTag.name)
            "
          >
            {{ hotTag.name }}
          </li>
        </ul>
      </div>
      <div class="type">
        <div
          class="item"
          :class="{ active: toggleActive }"
          @click="getSpecifiedTagsPlaylist(pageSize, offset, 'hot', currentCat)"
        >
          热门
        </div>
        <div
          class="item"
          :class="{ active: !toggleActive }"
          @click="getSpecifiedTagsPlaylist(pageSize, offset, 'new', currentCat)"
        >
          最新
        </div>
      </div>
    </div>
    <div
      class="list"
      v-loading="loadingPlaylist"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
    >
      <div
        class="item"
        v-for="(playlist, index) in getChosenPlaylist"
        :key="index"
        @click="getDetail(playlist)"
      >
        <div class="wrapper">
          <a>
            <div class="cover">
              <div class="image">
                <el-image
                  :src="playlist.coverImgUrl + '?param=300y300'"
                ></el-image>
              </div>
              <div class="count">
                <i class="arrow"></i>
                <span>{{ formatPlayCount(playlist.playCount) }}</span>
              </div>
            </div>
          </a>
        </div>
        <div class="info">
          <h2 class="name ellipsis-two">{{ playlist.name }}</h2>
        </div>
      </div>
    </div>
    <div class="page-wrap">
      <el-pagination
        background
        layout="prev,pager, next,total"
        :total="getChosenPlaylistCount"
        :page-size="pageSize"
        :current-page="currentPage"
        :page-count="pageCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
// 热门标签
import { formatPlayCount, URLencode } from "@/utils/index.js";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      loadingPlaylist: false,
      order: "hot", //"hot"  "new"
      toggleActive: true,
      showMenus: false,
      pageSize: 40, //每页显示歌单的数量
      currentPage: 1, //当前页码
      currentCat: "全部",
      checkindex: undefined,
    };
  },
  computed: {
    ...mapGetters([
      "getTags",
      "getCategories",
      "getSub",
      "getChosenPlaylist",
      "getChosenPlaylistCount",
    ]),
    getSonTags() {
      return function (catID) {
        return this.getSub.filter(function (item) {
          return item.category == catID;
        });
      };
    },
    //总页码数 =  总歌单数/ 每页歌单数
    pageCount() {
      return Math.ceil(this.total / this.pageSize);
    },
    // 偏移量，显示对应页码的歌单
    offset() {
      return this.pageSize * (this.currentPage - 1);
    },
    formatPlayCount() {
      return (playCount) => {
        return formatPlayCount(playCount);
      };
    },
  },
  mounted() {
    let cat = this.$route.query.cat;
    console.log("cat:" + cat);
    if (cat != undefined) {
      this.getSpecifiedTagsPlaylist(this.pageSize, this.offset, "hot", cat);
    } else {
      // 默认全部精选
      this.getSpecifiedTagsPlaylist(this.pageSize, this.offset, "hot", "全部");
    }
    //显示筛选菜单
    //热门标签
    this.$store.dispatch("getTags");
    // this.showPlaylistCat(); //全部标签
    this.$store.dispatch("getCategories");
  },
  methods: {
    getDetail(playlist) {
      //进入到歌单详情页
      this.$router.push({
        name: "Detail",
        query: { id: playlist.id },
      });
    },
    handleSizeChange(size) {
      console.log("size:" + size);
      this.pageSize = size;
      this.loadingPlaylist = true;
      this.$store.dispatch("getChosenPlaylist", {
        pageSize: this.pageSize,
        offset: this.offset,
        order: this.order,
        cat: this.currentCat,
      });
      this.loadingPlaylist = false;
    },
    handleCurrentChange(currentPage) {
      console.log("当前页码：" + currentPage);
      this.currentPage = currentPage;
      this.loadingPlaylist = true;
      this.$store.dispatch("getChosenPlaylist", {
        pageSize: this.pageSize,
        offset: this.offset,
        order: this.order,
        cat: this.currentCat,
      });
      this.loadingPlaylist = false;
    },
    //歌单查询
    getSpecifiedTagsPlaylist(pageSize, offset, order, cat) {
      this.$router.push({ name: "Playlist", query: { cat: cat } });

      this.toggleActive = !this.toggleActive;
      this.order = order;
      this.currentCat = URLencode(cat);
      this.$store.dispatch("getChosenPlaylist", {
        // pageSize: this.pageSize,
        pageSize: pageSize,
        // offset: this.offset,
        offset: offset,
        order: this.order,
        cat: this.currentCat,
      });
    },
  },
};
</script>
<style lang="stylus" scoped>
.fade-enter {
  opacity: 0;
  transform: translate3d(0, 30px, 0);
}

.fade-enter-active {
  transition: all 0.5s;
}

.fade-leave-to {
  opacity: 0;
  transform: translate3d(0, 30px, 0);
}

.fade-leave-active {
  transition: all 0.5s;
}

.playlist-wrap {
  .filter {
    width: 100%;
    height: 40px;
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    padding-right: 20px;
    border-radius: 5px 0px 5px 5px;

    .title {
      width: auto;
      height: 100%;
      padding: 0 5px 0 15px;
      background: $color-theme;
      border-radius: 5px 0px 5px 5px;
      color: #ffffff;
      cursor: pointer;
      position: relative;
      margin-right: 15px;
      position: relative;

      &::after {
        content: '';
        width: 1px;
        height: 20px;
        margin-left: 12px;
        background: #e7e7e7;
        opacity: 0;
      }

      > i {
        transform: rotate(90deg);
        margin-left: 10px;
        font-size: 18px;
      }

      .filter-box {
        width: 720px;
        height: 400px;
        overflow-y: scroll;
        background: rgba(255, 255, 255, 1);
        border-radius: 5px;
        color: #000;
        position: absolute;
        right: -631px;
        top: 50px;
        z-index: 100;
        padding: 15px 10px 0 16px;

        .item {
          margin-bottom: 20px;

          h2 {
            display: flex;
            align-items: center;
            font-size: 15px;
            margin-bottom: 15px;
            color: #161e27;

            i {
              font-size: 18px;
              margin-right: 5px;
              margin-top: -1px;
            }
          }

          ul {
            display: flex;
            flex-wrap: wrap;

            li {
              padding: 8px 18px;
              background: #f7f7f7;
              border-radius: 16px;
              margin: 0 10px 10px 0;
              cursor: pointer;
              font-size: 12px;
              color: #161e27;
              transition: all 0.4s;

              &:hover {
                color: #fff;
                background: $color-theme;
              }

              &.active {
                color: #fff;
                background: $color-theme;
              }
            }
          }
        }
      }

      &:hover {
        // border: 1px solid #cccccc;
      }
    }

    .hot-tag {
      flex: 1;

      ul {
        li {
          margin: 0 5px;
          padding-right: 10px;
          cursor: pointer;

          &:hover {
            color: #888;
          }

          &.active {
            color: $color-theme;
          }
        }
      }
    }

    .type {
      display: flex;

      .item {
        margin-left: 20px;
        background: #f7f7f7;
        color: #161e27;
        padding: 5px 10px;
        font-size: 12px;
        border-radius: 3px;
        transition: all 0.4s;
        cursor: pointer;

        &.active {
          background: $color-theme;
          color: #fff;

          &:hover {
            color: #fff;
          }
        }

        &:hover {
          color: #666;
        }
      }
    }
  }
}

.list {
  display: flex;
  flex-wrap: wrap;
  margin: 0 -15px;

  .item {
    flex: 0 0 12.5%;
    max-width: 12.5%;
    padding: 0 15px 30px;
    cursor: pointer;

    &.two {
      flex: 0 0 50%;
      max-width: 50%;
    }

    .wrapper {
      position: relative;
      padding-right: 10px;

      &:before, &:after {
        content: '';
        width: 100%;
        height: 100%;
        background-color: #d9d9d9;
        position: absolute;
        top: 0;
        transition: all 0.4s;
      }

      &:before {
        right: 6px;
        transform: scale(0.85);
        transform-origin: 100% 50%;
        z-index: 2;
        border-radius: 2px;
      }

      &:after {
        right: 4px;
        transform: scale(0.73);
        transform-origin: 100% 50%;
        z-index: 1;
        border-radius: 2px;
        opacity: 0.5;
      }

      .cover {
        position: relative;
        z-index: 2;
        padding-top: 100%;
        border-radius: 2px;
        background-color: #d9d9d9;

        .image {
          position: absolute;
          top: 0;
          left: 0;
          overflow: hidden;
          width: 100%;
          height: 100%;
          border-radius: 4px;

          &:after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            border-radius: 2px;
            -webkit-box-shadow: 1px 1px 0 rgba(0, 0, 0, 0.05) inset, -1px -1px 0 rgba(0, 0, 0, 0.05) inset;
            box-shadow: inset 1px 1px 0 rgba(0, 0, 0, 0.05), inset -1px -1px 0 rgba(0, 0, 0, 0.05);
          }

          img {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 100%;
          }
        }

        .count {
          position: absolute;
          right: 16px;
          top: 1px;
          height: 24px;
          padding-left: 9px;
          background: url('https://img.alicdn.com/tfs/TB1xEGRub9YBuNjy0FgXXcxcXXa-268-48.png') no-repeat 0; // img.alicdn.com/tfs/TB1xEGRub9YBuNjy0FgXXcxcXXa-268-48.png) no-repeat 0;
          background-size: cover;
          color: #fff;
          font-size: 12px;
          font-weight: 700;
          line-height: 24px;
          display: flex;
          align-items: center;
          justify-content: center;

          .arrow {
            display: block;
            width: 0;
            height: 0;
            border-style: solid;
            border-width: 4px 0 4px 6px;
            border-color: transparent transparent transparent #ffffff;
            margin-right: 5px;
          }

          &:after {
            content: '';
            position: absolute;
            right: -14px;
            top: 0;
            width: 14px;
            height: 24px;
            background: url('https://img.alicdn.com/tfs/TB1xEGRub9YBuNjy0FgXXcxcXXa-268-48.png') no-repeat 100%; // img.alicdn.com/tfs/TB1xEGRub9YBuNjy0FgXXcxcXXa-268-48.png) no-repeat 100%;
            background-size: cover;
          }
        }
      }
    }

    .info {
      margin-top: 15px;

      .name {
        font-size: 14px;
      }
    }

    &:hover {
      .wrapper {
        &:before, &:after {
          background: rgba(250, 40, 0, 0.1);
        }

        &:before {
          right: 4px;
        }

        &:after {
          right: 0px;
        }
      }
    }
  }
}
</style>